{% extends "layout/manage.html" %}
{% load static %}

{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/editor.md/css/editormd.preview.css' %}">
    <style>
        .panel-body {
            padding: 0;
        }

        .panel-default {
            margin-top: 10px;
        }

        .panel-default .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;

        }

        .title-list {
            border-right: 1px solid #dddddd;
            min-height: 500px;
        }

        .title-list ul {
            padding-left: 15px;
        }

        .content {
            border-left: 1px solid #dddddd;
            min-height: 600px;
            margin-left: -1px;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="panel panel-default">
        <div class="panel-heading ">

            <div>
                <i class="fa fa-book" aria-hidden="true"> wiki文档</i>
            </div>
            <div class="function">
                {% if wiki_object %}
                    <a type="button" class="btn btn-success btn-xs"
                       href="{% url 'web:manage:wiki_add' project_id=request.tracer.project.id %}">
                        <i class="fa fa-plus-circle" aria-hidden="true"> 新建</i>
                    </a>
                    <a type="button" class="btn btn-primary btn-xs"
                       href="{% url 'web:manage:wiki_edit' project_id=request.tracer.project.id  wiki_id=wiki_object.id %}">
                        <i class="fa fa-edit" aria-hidden="true"> 编辑</i>
                    </a>
                    <a type="button" class="btn btn-danger btn-xs"
                       href="{% url 'web:manage:wiki_delete' project_id=request.tracer.project.id  wiki_id=wiki_object.id %}">
                        <i class="fa fa-trash" aria-hidden="true"> 删除</i>
                    </a>
                {% else %}
                    <a type="button" class="btn btn-success btn-xs"
                       href="{% url 'web:manage:wiki_add' project_id=request.tracer.project.id %}">
                        <i class="fa fa-plus-circle" aria-hidden="true">新建</i>
                    </a>
                {% endif %}
            </div>
        </div>
        <div class="panel-body">
            <div class="col-sm-3 title-list">
                <ul id="catalog">

                </ul>
            </div>
            <div class="col-sm-9 content">
                {% if wiki_object %}
                    <div id="previewMardown">
                        <textarea> {{ wiki_object.content }}</textarea>
                    </div>
                {% else %}
                    <div style="text-align: center; margin-top: 50px">
                        <h4>《{{ request.tracer.project.title }} 》wiki文档</h4>
                        <a href="{% url 'web:manage:wiki_add' project_id=request.tracer.project.id %}">
                            <i class="fa fa-plus-circle" aria-hidden="true">新建文档</i>
                        </a>
                    </div>
                {% endif %}
            </div>

        </div>
    </div>
{% endblock %}

{% block js %}
    <script src="{% static 'plugin/editor.md/editormd.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md/lib/flowchart.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md/lib/jquery.flowchart.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md/lib/marked.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md/lib/prettify.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md/lib/raphael.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md/lib/sequence-diagram.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md/lib/underscore.min.js' %}"></script>

    <script>
        //
        var WIKI_DETAIL_URL = "{% url 'web:manage:wiki'  project_id=request.tracer.project.id %}";
        $(function () {
            initCatalog();
            initPreviewMd();
        });

        function initCatalog() {
            $.ajax({
                url: "{% url 'web:manage:wiki_catalog' project_id=request.tracer.project.id %}",
                type: "GET",
                dataType: "JSON",
                success: function (res) {
                    if (res.status) {
                        $.each(res.data, function (index, item) {
                            // 如果有parent_id
                            var href = WIKI_DETAIL_URL + "?wiki_id=" + item.id;
                            if (item.parent) {
                                var li = $("<li>").attr("id", "id_" + item.id).append($("<a>").attr("href", href).text(item.title)).append($("<ul>"));
                                $("#id_" + item.parent).children("ul").append(li)
                            } else {
                                var li = $("<li>").attr("id", "id_" + item.id).append($("<a>").attr("href", href).text(item.title)).append($("<ul>"));
                                $("#catalog").append(li);
                            }
                        })
                    } else {
                        alert("初始化目录失败")
                    }
                }
            })
        }

        function initPreviewMd() {
            editormd.markdownToHTML("previewMardown",{
                htmlDecode: "style,script,iframe",
            })
        }
    </script>
{% endblock %}








